package com.overseasShopping.Action;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.overseasShopping.util.RandomImageGenerator;

public class VerificationAtion extends ActionSupport{
	private int randomStrNum=5;
	private int width=130;
	private int height=40;


	@Override
	public String execute() throws Exception {
	      HttpServletResponse response = ServletActionContext.getResponse();
	           
		//获取随机字符串
	      try {
	    	  String randomStr = RandomImageGenerator.random(randomStrNum);
	    	  Map<String, Object> gap = ActionContext.getContext().getSession();
	    	  
	    	  //设置参数
	    	  gap.put("randomStr", randomStr);
	    	  //设置响应类型,输出图片客户端不缓存
	    	  response.setDateHeader("Expires", 1L);  
	    	  response.setHeader("Cache-Control", "no-cache, no-store, max-age=0");
	    	  response.addHeader("Pragma", "no-cache");
	    	  response.setContentType("image/jpeg"); 
	    	  //输出到页面
	    	  RandomImageGenerator.render(randomStr, response.getOutputStream(), width, height);
	    	  
		} catch (Exception e) {
			System.out.println(e.getMessage());  
			e.printStackTrace();  

		}
	      
	  	response.getOutputStream().flush(); //刷新到页面生成图片 
	  	response.getOutputStream().close(); //关闭writer 
		return null;
	}
	
}
